#!/bin/bash

USER_DEF_CMD=$1


echo USER_DEF_CMD=$USER_DEF_CMD

# 替换成自己esp-idf安装的路径
# IDF_51_PATH="/home/yejinghang/ye_share/01.tools/10.esp_idf/esp-idf"

# IDF_51_PATH = ${IDF_PATH}

echo IDF52_PATH=${IDF52_PATH}
echo "Start Compile"

. ${IDF52_PATH}/export.sh
echo "Added IDF_PATH"

# idf.py set-target esp32s3

if [ "$USER_DEF_CMD" = "clean" ];then
	rm -rf build
	idf.py set-target esp32s3
	idf.py menuconfig

    # idf.py erase_flash
elif [ "$USER_DEF_CMD" = "menu" ];then

    idf.py menuconfig
elif [ "$USER_DEF_CMD" = "saveConfig" ];then

    idf.py save-defconfig
    
    echo "********************** SAVE CONFIG SUCCESS! **************************"

    exit 0
fi


idf.py build


idf.py size


echo "

    ███╗   ██╗██████╗ ██╗
    ████╗  ██║██╔══██╗██║
    ██╔██╗ ██║██████╔╝██║
    ██║╚██╗██║██╔══██╗╚═╝
    ██║ ╚████║██████╔╝██╗
    ╚═╝  ╚═══╝╚═════╝ ╚═╝
    "
echo "********************** #DEBUG# COMPILE SUCCESS! **************************"

echo "do you want to flash bin?(y/n)"

read will_flash_bin

if [ "$USB_NUM" -gt "1" ];then
    echo "There are $USB_NUM usb port in PC:"
    ls /dev/ttyUSB*|awk '{printf "%s.%s  ", NR, $0}'
    echo ""
    echo "Choose the number of usb port below:(1/2/3/...)"
    read portnum
    USB_PORT=$(ls /dev/ttyUSB*|awk -vport=$portnum 'NR==port{printf "%s", $0}')
else
    echo "There is only 1 usb port in PC, choose this port automatically."
    USB_PORT=$(ls /dev/ttyUSB*)
fi


echo "usb port:"$USB_PORT

# idf.py -p $USB_PORT -b 921600 flash

idf.py flash

idf.py monitor



